﻿using BigEgg.Framework.Foundation;
using System;
using System.ComponentModel;
using System.ComponentModel.DataAnnotations;

namespace FMStudio.Domain.SQLite
{
    [MetadataType(typeof(IMarket))]
    public partial class Market : IMarket, IDataErrorInfo
    {
        #region Implement IDataErrorInfo Interface
        [NonSerialized]
        private readonly DataErrorInfoSupport dataErrorInfoSupport;

        string IDataErrorInfo.Error { get { return this.dataErrorInfoSupport.Error; } }

        string IDataErrorInfo.this[string memberName] { get { return this.dataErrorInfoSupport[memberName]; } }
        #endregion


        public Market()
        {
            this.dataErrorInfoSupport = new DataErrorInfoSupport(this);

            //  Default value
            this.Branch = string.Empty;
            this.Name = string.Empty;
            
            this.CityReference.AssociationChanged += CityReferenceAssociationChanged;
        }


        #region Implement IMarket Interface
        ICity IMarket.City
        {
            get { return this.City; }
            set { this.City = value as City; }
        }
        #endregion


        #region Private Methods
        private void CityReferenceAssociationChanged(object sender, CollectionChangeEventArgs e)
        {
            // The navigation property doesn't support the PropertyChanged event. We have to raise it ourselves.
            OnPropertyChanged("City");
            this.CityID = this.City.ID;
        }
        #endregion
    }
}
